package com.makejl.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import com.sun.org.apache.xerces.internal.util.URI;

/**
 * 
 * @author Beni
 * 
 */
public class Html2Pdf {

	public static void main(String[] args) throws Exception {
		URL url = new URL(
				"http://www.html-to-pdf.net/free-online-pdf-converter.aspx");

		String __EVENTVALIDATION="";
		String __VIEWSTATE="";
		String urlPath="www.youmo365.com/chinahrResume.htm";
		// use proxy
		// InetSocketAddress addr = new InetSocketAddress("10.167.39.11", 8080);
		// Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
		// URLConnection conn = url.openConnection(proxy);

		InputStream is=url.openStream();
		 InputStreamReader isr = new InputStreamReader(is, "gb2312");
		 BufferedReader inBuffer = new BufferedReader(isr);
		 String temp = "";
		 while ((temp = inBuffer.readLine()) != null) {
			 
			 if(temp.indexOf("id=\"__VIEWSTATE\"")!=-1){
				 String [] strs=temp.split("\"");
				 System.out.println(strs[strs.length-2]);
				 
				 __VIEWSTATE=URLEncoder.encode(strs[strs.length-2]);
				 System.out.println(__VIEWSTATE);
			 }
			 
			 if(temp.indexOf("id=\"__EVENTVALIDATION\"")!=-1){
				 
				 String [] strs=temp.split("\"");
				 System.out.println(strs[strs.length-2]);
				 
				 __EVENTVALIDATION=URLEncoder.encode(strs[strs.length-2]);
				 System.out.println(__EVENTVALIDATION);
				 break;
			 }
		 }
		 
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();

		conn.setRequestMethod("POST");
		conn.setRequestProperty("Host", "www.html-to-pdf.net");
		conn.setRequestProperty("Content-Type",
				"application/x-www-form-urlencoded");
		conn.setDoOutput(true);
//		conn.setDoInput(true);
		OutputStream outputStream = conn.getOutputStream();
		PrintWriter out = new PrintWriter(outputStream);

		// proxy
		// out.print("__EVENTTARGET=ctl00%24ContentPlaceHolder1%24BtnExport&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTEzOTE5OTA5NjkPZBYCZg9kFgICAw9kFgQCCQ8WAh4EVGV4dAUgRnJlZSBvbmxpbmUgVVJMIHRvIFBERiBDb252ZXJ0ZXJkAgoPFgIfAAVvQ29udmVydCBhbnkgd2ViIHBhZ2UgdG8gUERGIHVzaW5nIG91ciBmcmVlIG9ubGluZSBQREYgQ29udmVydGVyLiBGYXN0IGFuZCByZWxpYWJsZSBQREYgZ2VuZXJhdGlvbiBmcm9tIGFueSBVUkwuZGT8%2Fr1w9wN73WEPb8WGYVtkTWrnng%3D%3D&__EVENTVALIDATION=%2FwEWAwLFm8WGCgK9pb%2BCBgK3yNn6DGyanDIVKjRAGLPPW%2Fehjrt3ySKi&ctl00%24ContentPlaceHolder1%24TxtURL=www.baidu.com");
		out
				.print("__EVENTTARGET=ctl00%24ContentPlaceHolder1%24BtnExport&__EVENTARGUMENT=&__VIEWSTATE="+__VIEWSTATE+"&__EVENTVALIDATION="+__EVENTVALIDATION+"&ctl00%24ContentPlaceHolder1%24TxtURL="+urlPath);
//		__EVENTVALIDATION
		out.flush();
		out.close();
		out = null;

		System.out.println(conn.getResponseCode());
		
		InputStream in = conn.getInputStream();

		File f = new File("e:/a.pdf");
		f.createNewFile();

		OutputStream out1 = new FileOutputStream(f);
		byte buf[] = new byte[1024];
		int len;
		while ((len = in.read(buf)) > 0)
			out1.write(buf, 0, len);
		out1.close();
		in.close();
		System.out.println("File is created........");
	}
}
